/*
 * Copyright (c) 2019. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.dyw.player;

import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.AudioHeader;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import org.jaudiotagger.tag.TagField;
import org.jaudiotagger.tag.datatype.Artwork;
import org.jaudiotagger.tag.id3.ID3v23Frame;

import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Iterator;
import java.util.List;

public class MetadataInfo {

    public static AudioFileInfo metadataBuild(File file) throws Exception{
        AudioFile f = AudioFileIO.read(file);
        Tag tag = f.getTag();
        AudioHeader audioHeader = f.getAudioHeader();
        List<Artwork> artworkList = tag.getArtworkList();

        AudioFileInfo info = new AudioFileInfo();

        for (Artwork artwork : artworkList) {
            BufferedImage image = artwork.getImage();
            info.setIcon(image);
        }
        // 作者
        info.setArtist(tag.getFirst(FieldKey.ARTIST));
        // 专辑
        info.setAlbum(tag.getFirst(FieldKey.ALBUM));
        // 标题
        info.setTitle(tag.getFirst(FieldKey.TITLE));
        // 时长
        info.setTrackLength(audioHeader.getTrackLength());

        return info;
    }
}
